/**
 * Created by ㅋㅋElsa on 2017/9/24.
 */

layui.define(['form', 'layer','jquery'], function(exports){
    var form = layui.form(),
        $ = layui.jquery,
        layer = layui.layer;

    $.ajax({
        type: "GET",
        url: contextPath + "/personal/info",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            $('#username').attr("value",data.username);
        },
        error: function (e) {

        }
    });

    $('#oldpsd').on('blur',function () {
        webChange();
    });

    $('#yespsd').on('blur',function () {
        webChange();
    });

    $('#newpsd').on('blur',function () {
        webChange();
    });

    function webChange(){
    	if($('#newpsd').val() && $('#oldpsd').val() && $('#oldpsd').val()==$('#newpsd').val()){
    		$('#all-info').html("*新密码和旧密码输入相同");
    	}else if($('#newpsd').val() && $('#yespsd').val() && $('#yespsd').val()!=$('#newpsd').val()){
    		$('#all-info').html("*两次密码输入不相同");
    	}else{
    		$('#all-info').html("");
    	}
    }

    form.on('submit(caseglyx)', function(data) {
    	var nameRegex=/^(.){6,12}$/;

        if(!$('#oldpsd').val()){
            $('#all-info').html("*请输入旧密码");
        }else if(!$('#newpsd').val()){
			$('#all-info').html("*请输入新密码");
        }else if(!$('#yespsd').val()){
        	$('#all-info').html("*请输入确认密码");
        }else if($('#oldpsd').val()&&!nameRegex.test($('#oldpsd').val())){
			$("#all-info").html("*密码长度为6-12字母和数字");
        }else if($('#newpsd').val()&&!nameRegex.test($('#newpsd').val())){
			$("#all-info").html("*密码长度为6-12字母和数字");
        }else{
            var ChangePsd = new Object();
            ChangePsd.oldpsd = $('#oldpsd').val();
            ChangePsd.password = $("#newpsd").val();

            console.log(JSON.stringify(ChangePsd));

            $.ajax({
                type:"PUT",
                url:contextPath+"/personal/updatepsw",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(ChangePsd),
                dataType:"json",
                success:function (data) {
                    console.log(data.message);
                    if(data.message == "密码修改成功!"){
                        layer.closeAll();
                        layer.msg('密码修改成功!<br>页面将在1秒后刷新...');
                        setTimeout(function(){window.location.reload();},1000);
                    } else {
                        layer.msg(data.message, {
                            time:false,
                            btn:"确定",
                            btnAlign: 'c'
                        });
                        $("#all-info").html("*"+data.message);
                    }
                },
                error:function (e) {
                }
            });
        }

        return false;
    });

    exports('ChangePsd', {}); //注意，这里是模块输出的核心，模块名必须和use时的模块名一致
});